package sites.manager.admin.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import sites.manager.common.kit.HashKit;
import sites.manager.common.kit.Ret;
import sites.manager.common.kit.StrKit;
import sites.manager.common.mapper.AccountMapper;
import sites.manager.common.model.Account;

@Component
public class AccountService extends ServiceImpl<AccountMapper, Account> {
	
	@Autowired
	private AccountMapper mapper;
	
	/**
	 * 查询数据库中的account记录
	 * 
	 * @param account
	 * @return
	 */
	public Account getAccount(Account account) {
		LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(StrKit.notBlank(account.getUserName()), Account::getUserName, account.getUserName());
		return getOne(wrapper);
	}
	
	/**
	 * account登录校验
	 * 
	 * @param account
	 * @return
	 */
	public Ret login(Account account) {
		Ret ret = Ret.create();
		
		Account temp = getAccount(account);
		if(temp == null) {
			ret.setFail();
			ret.setMsg("用户名或密码错误，请重试！");
		} else {
			String pwd = temp.getPassword();
			if(!pwd.equals(HashKit.md5(account.getPassword()))) {
				ret.setFail();
				ret.setMsg("用户名或密码错误，请重试！");
			} else {
				ret.setOk();
				ret.setData(account);
			}
		}
		
		return ret;
	}

}
